<?php

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\order;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;
use Imi\Config;

/**
 * 普通商品订单
 * @Auth(name="token",nologin={"aftertype_list"})
 * @Controller("/order.order/")
 */
class Order extends OrderBean
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 售后类型列表
     *
     * @param string $type_status 类型:refund=退款,return=退货,change=换货,other=其他
     * @return void
     * @Action
     */
    public function aftertype_list($type_status = null)
    {
        $$where = [];
        if ($type_status)
            $where['type_status'] = $type_status;
        return $this->success($type_status, Db::name('aftertype')->whereEx($where)->orderRaw('weigh desc')->select()->getArray());
    }

    /**
     * 获取我的子订单列表
     *
     * @param integer|null $page 1
     * @param integer|null $limit 10
     * @param string $status 订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
     * @param string $comment_status 评价状态:0=未评价,1=已评价
     * @param string $dispatch_status 发货状态:0=未发货,1=已发货,2=已收货
     * @param string $aftersale_status 售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成,3=全部
     * @param string $order 订单号
     * @param string $search 搜索
     * @return void
     * @Action
     */
    public function get_my_order_item_list(?int $page = 1, ?int $limit = 10, $status = null, $comment_status = null, $dispatch_status = null, $aftersale_status = null, $order = null, $search = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $where['user_id'] = $this->auth->user()->id;



        $order_ids_arr = [];



        if ($order) {
            $where0['user_id'] = $this->auth->user()->id;
            $where0['order_sn'] = ['like', '%' . $order . '%'];
            $order_ids = Db::name('order')
                ->whereEx($where0)
                ->fieldRaw('id')
                ->distinct()
                ->column('id');
            if ($order_ids) {
                $order_ids_arr = $order_ids;
            } else {
                // return $this->success('ok', []);
            }
             $where['and'] = ['goods_sku_text' => ['like', '%' . $search . '%'], 'or' => ['goods_title' => ['like', '%' . $search . '%']]];
        }

        if ($status !== null && $status !== '') {
            $where1['status'] = $status;
            $where1['user_id'] = $this->auth->user()->id;
            $order_ids = Db::name('order')->whereEx($where1)->fieldRaw('id')->distinct()->column('id');
            // var_dump($where1);
            if ($order_ids) {
                $order_ids_arr = array_intersect($order_ids_arr, $order_ids);
            } else {
                return $this->success('ok', []);
            }
        }




        //评论
        if ($comment_status !== null && $comment_status !== '') {
            if ($comment_status === '0') {
                $where['status'] = '2';
                $where2['user_id'] = $this->auth->user()->id;
                $order_ids = Db::name('order')->whereEx($where2)->fieldRaw('id')->distinct()->column('id');
                if ($order_ids) {
                    $order_ids_arr = array_intersect($order_ids_arr, $order_ids);
                } else {
                    return $this->success('ok', []);
                }
            }
            $where['comment_status'] = $comment_status;
        }

        //售后
        if ($aftersale_status) {
            // $where3['user_id'] = $this->auth->user()->id;
            // if ($aftersale_status != '3') {
            //     $where['aftersale_status'] = $aftersale_status;
            // }

            // $order_ids = Db::name('order_aftersale')->whereEx($where3)->fieldRaw('order_id')->distinct()->column('order_id');
            // if ($order_ids) {
            //     $where['order_id'] = ['in', $order_ids];
            //     $order_ids_arr = array_intersect($order_ids_arr, $order_ids);
            // }
            if ($aftersale_status != '3') {
                $where['aftersale_status'] = $aftersale_status;
            } else {
                $where3['user_id'] = $this->auth->user()->id;
                $order_ids = Db::name('order_aftersale')->whereIsNull('deletetime')->whereEx($where3)->fieldRaw('order_id')->distinct()->column('order_id');
                if ($order_ids) {
                    $order_ids_arr = array_intersect($order_ids_arr, $order_ids);
                } else {
                    return $this->success('ok', []);
                }
            }
        } else {
            $where['aftersale_status'] = '0';
        }


        if ($dispatch_status != null && $dispatch_status != '') {
            $where['dispatch_status'] = $dispatch_status;
        }



        if ($order_ids_arr) {
            $where['order_id'] = ['in', array_unique($order_ids_arr)];
        }
        // var_dump($where);

        // if ($search) {
        //     $where['and'] = ['goods_sku_text' => ['like', '%' . $search . '%'], 'or' => ['goods_title' => ['like', '%' . $search . '%']]];
        // }

        $list = Db::name('order_item')->whereEx($where)->page($page, $limit)->whereIsNull('deletetime')->orderRaw('createtime desc,id desc')->select()->getArray();
        foreach ($list as $key => &$val) {
            $val = $this->ChangeOrder2($val);
        }
        return $this->success('ok', $list);
    }











    /**
     * 获取我的主订单列表
     *
     * @param integer|null $page 1
     * @param integer|null $limit 10
     * @param string $status 订单状态:-2=交易关闭,-1=已取消,0=未支付,1=已支付,2=已完成
     * @param string $comment_status 评价状态:0=未评价,1=已评价
     * @param string $dispatch_status 发货状态:0=未发货,1=已发货,2=已收货
     * @param string $aftersale_status 售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成,3=全部
     * 
     * @return void
     * @Action
     */
    public function get_main_order_list(?int $page = 1, ?int $limit = 10, $status = null, $comment_status = null, $dispatch_status = null, $aftersale_status = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $where['user_id'] = $this->auth->user()->id;

        if ($status)
            $where['status'] = $status;

        //评论
        if ($comment_status !== null) {
            if ($comment_status === '0') {
                $where['status'] = '1';
            }
            $where2['comment_status'] = $comment_status;
            $where2['user_id'] = $this->auth->user()->id;
            $order_ids = Db::name('order_item')->whereEx($where2)->fieldRaw('order_id')->distinct()->column('order_id');
            if ($order_ids)
                $where['id'] = ['in', $order_ids];
        }

        //售后
        if ($aftersale_status) {
            $where3['user_id'] = $this->auth->user()->id;
            if ($aftersale_status != '3') {
                $where3['aftersale_status'] = $aftersale_status;
            }

            $order_ids = Db::name('order_aftersale')->whereEx($where3)->whereIsNull('deletetime')->fieldRaw('order_id')->distinct()->column('order_id');
            if ($order_ids)
                $where['id'] = ['in', $order_ids];
        }

        $list = Db::name('order')->whereEx($where)->page($page, $limit)->orderRaw('createtime desc')->select()->getArray();
        foreach ($list as $key => &$val) {
            $val = $this->ChangeOrder($val);
        }
        return $this->success('ok', $list);
    }




    /**
     * 单个评价
     *
     * @param int $order_id 总订单id
     * @param integer|null $order_item_id
     * @param string $content 评价内容
     * @param integer $level 评分等级
     * @param string $images 图片
     * @return void
     * @Action
     */
    public function comment(?int $order_id = null, ?int $order_item_id = null, $content = null, $level = 5, $images = null)
    {
        $where['order_id'] = $order_id;
        $where['id'] = $order_item_id;
        $where['user_id'] = $this->auth->user()->id;

        $res = Db::name('order_item')->whereEx($where)->find();
        // var_dump($where);
        if ($res) {
            if ($res['comment_status'] == '1')
                return $this->error('您已评论过该订单');


            $data['order_id'] = $order_id;
            $data['order_item_id'] = $order_item_id;
            $data['user_id'] = $this->auth->user()->id;
            $order =  Db::name('order_item')->where('id', '=', $order_item_id)->find();
            $data['goods_id'] = $order['goods_id'];
            $data['goods_sku_price_id'] = $order['goods_sku_price_id'];
            $data['store_id'] = $order['store_id'];

            $data['level'] = $level;
            $data['content'] = $this->Xss($content);
            if (!empty($images))
                $data['images'] = $images;
            $data['status'] = 'show'; //hidden
            $data['createtime'] = time();

            $id = Db::name('goods_comment')->insert($data)->getLastInsertId();
            Db::name('order_item')->where('id', '=', $res['id'])->update(['comment_status' => '1']);
            return $this->success('评论成功', $id);
        } else {
            return $this->error('没有该订单');
        }

        return $this->error('您已评论');
    }

    /**
     * 删除商品评论
     *
     * @param integer|null $id 评论id
     * @return void
     * @Action
     */
    public function del_comment(?int $id = null)
    {
        $where['id'] = $id;
        $where['user_id'] = $this->auth->user()->id;

        $val = Db::name('goods_comment')->whereEx($where)->whereIsNull('deletetime')->find();
        if (!$val) {
            return $this->error('没有该评论');
        }
        Db::name('goods_comment')->whereEx($where)->update(['deletetime' => time()]);
        return $this->success('删除成功');
    }


    /**
     * 主订单详情👔
     *
     * @param int $id 主订单id
     * @return void
     * @Action
     */
    public function main_order_detail($id)
    {
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order = Db::name('order')
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->find();
        if ($order) {
            $order = $this->ChangeOrderDetail($order);
        }
        return $this->success('order', $order);
    }

    /**
     * 子订单详情👔
     *
     * @param int $id 订单id
     * @return void
     * @Action
     */
    public function order_item_detail($id)
    {
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order = Db::name('order_item')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->select()->get();
        if ($order) {
            $order = $this->ItemChangeOrderDetail($order);
        }
        return $this->success('order', $order);
    }


    /**
     * 删除子订单👔
     *
     * @param int $id 订单id
     * @return void
     * @Action
     */
    public function del_order_item($id)
    {
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order = Db::name('order_item')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->select()->get();
        if (!$order) {
            return $this->error('没有该订单');
        }
        Db::name('order_item')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->update(['deletetime' => time()]);

        return $this->success('删除成功');
    }



    /**
     * 子订单确认收货👔
     *
     * @param int $id 订单id
     * @return void
     * @Action
     */
    public function order_item_confirm($id)
    {
        $where['id'] = $id;
        $where['user_id'] =  $this->auth->user()->id;
        $order_item = Db::name('order_item')
            // ->whereEx($this->whereCom)
            ->whereEx($where)
            ->whereIsNull('deletetime')
            ->select()->get();
        // if ($order_item) {
        //     $order = $this->ItemChangeOrderDetail($order_item);
        // }
        if (!$order_item)
            return $this->error('没有该订单');
        if ($order_item['dispatch_status'] == '1') {
            $update['dispatch_status'] = '2'; //
            // $update['status'] = '2';

            // var_dump($order_item);
            $order = Db::name('order')->where('id', '=', $order_item['order_id'])->find();
            //直接加余额 积分 贝壳退回
            $订单操作['order_id'] = $order_item['order_id'];
            $订单操作['order_item_id'] = $order_item['id'];
            $订单操作['oper_type'] = 'user'; //user','store','admin','system'
            $订单操作['oper_id'] = $this->auth->user()->id;
            $订单操作['order_status'] = $order['status']; //订单状态
            $订单操作['dispatch_status'] = '2'; //发货状态:0=未发货,1=已发货,2=已收货
            $订单操作['comment_status'] = $order_item['comment_status']; //评价状态:0=未评价,1=已评价
            $订单操作['aftersale_status'] = $order_item['aftersale_status']; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
            $订单操作['refund_status'] = $order_item['refund_status']; //退款状态:-1=拒绝退款,0=未退款,1=同意
            $订单操作['remark'] = '用户确认收货';
            $订单操作['createtime'] = time();
            $订单操作['updatetime'] = time();
            $this->UpdateOrderAction($订单操作);


            Db::name('order_item')->whereEx($where)->update($update);
            $this->orderItemConfirm($id);
        } else {
            switch ($order_item['dispatch_status']) {
                    //发货状态:0=未发货,1=已发货,2=已收货
                case '0':
                    return  $this->error('还未发货');
                    break;
                case '2':
                    return $this->error('已收货了哦');
                    break;
                default:
                    # code...
                    break;
            }
        }

        return $this->success('确认成功', $order);
    }





    /**
     * 取消总订单
     *
     * @param int $order_id 总订单id
     * @return void
     * @Action
     */
    public function cancel_order_total(?int $order_id)
    {
        $this->CancelTotalOrder($order_id);
        return $this->success('取消成功', $order_id);
    }

    /**
     * 取消订单
     *
     * @param int $order_id 订单id
     * @return void
     */
    public function cancel_order($order_id)
    {
    }



    protected function ChangeOrder($val)
    {
        $val['store'] = $this->getStoreData5($val['store_id']);
        $val['order_item'] = Db::name('order_item')->where('order_id', '=', $val['id'])->select()->getArray();
        return $val;
    }

    protected function ChangeOrder2($val)
    {
        $val['store'] = $this->getStoreData5($val['store_id']);
        $val['order'] = Db::name('order')->where('id', '=', $val['order_id'])->fieldRaw('order_sn,status,dispatch_amount')->find();
        return $val;
    }

    protected function ItemChangeOrderDetail($val)
    {
        $val['store'] = $this->getStoreData5($val['store_id']);
        if ($val['dispatch_time'] > 0)
            $val['dispatch_date'] = date('Y-m-d H:i:s', $val['dispatch_time']);
        // $val['order_item'] = Db::name('order_item')->where('order_id', '=', $val['id'])->select()->getArray();
        $order = Db::name('order')->where('id', '=', $val['order_id'])->find();
        if ($order) {
            $order['date'] = date('Y-m-d H:i:s', $order['createtime']);
            if ($order['paytime'] > 0)
                $order['paydate'] = date('Y-m-d H:i:s', $order['paytime']);
        }
        $val['order'] = $order;
        return $val;
    }

    protected function ChangeOrderDetail($val)
    {
        $val['store'] = $this->getStoreData5($val['store_id']);
        $val['order_item'] = Db::name('order_item')->where('order_id', '=', $val['id'])->select()->getArray();
        return $val;
    }









    // /**
    //  * 子订单申请退款
    //  *
    //  * @param int $id 订单id
    //  * @param float $refund_fee 退款金额
    //  * @param float $refund_score 退款积分
    //  * @param float $refund_shell 退款贝壳
    //  * @param int $aftertype_id 售后类型
    //  * @param string $ps 留言
    //  * @return void
    //  * @Action
    //  */
    // public function item_apply_refund(?int $id, ?float $refund_fee = null, ?float $refund_score = null, ?float $refund_shell = null, ?int $aftertype_id = null, ?string $ps = null)
    // {
    //     $where['id'] = $id;
    //     $where['user_id'] =  $this->auth->user()->id;
    //     $order_item = Db::name('order_item')
    //         // ->whereEx($this->whereCom)
    //         ->whereEx($where)
    //         ->whereIsNull('deletetime')
    //         ->find();
    //     if (!$order_item)
    //         return $this->error('没有该订单');
    //     $order_data = Db::name('order')->where('id', '=', $order_item['order_id'])->find();



    //     $data['user_id'] = $this->auth->user()->id;
    //     $data['order_id'] = $order_data['id'];
    //     $data['order_item_id'] = $id;
    //     $data['type'] = 'refund'; //类型:refund=退款,return=退货,other=其他
    //     $res = Db::name('order_aftersale')->whereEx($data)->find();
    //     if ($res) {
    //         if ($res['aftersale_status'] != '-1')
    //             return $this->error('该订单已申请过退款');
    //     }

    //     if ($refund_fee > $order_item['item_pay_fee'] * $order_item['goods_num'] + $order_data['dispatch_amount']) {
    //         return $this->error('大于购买商品的价格的钱', $order_item['item_pay_fee'] * $order_item['goods_num'] + $order_data['dispatch_amount']);
    //         $data['refund_fee'] = $refund_fee;
    //     } elseif (!$refund_fee || $refund_fee < 0) {
    //         // return $this->error('请输入退款金额');
    //         $data['refund_fee'] = $order_item['item_pay_fee'] * $order_item['goods_num'];
    //     }

    //     if ($refund_shell > $order_item['item_shell_fee'] * $order_item['goods_num']) {
    //         return $this->error('大于购买商品的贝壳', $order_item['item_shell_fee'] * $order_item['goods_num']);
    //     } else {
    //         $data['refund_shell'] = $order_item['item_shell_fee'] * $order_item['goods_num'];
    //     }

    //     if ($refund_score > $order_item['item_score_fee'] * $order_item['goods_num']) {
    //         return $this->error('大于购买商品的积分', $order_item['item_score_fee'] * $order_item['goods_num']);
    //     } else {
    //         $data['refund_score'] = $order_item['item_score_fee'] * $order_item['goods_num'];
    //         // $data['refund_score'] = $refund_score?$refund_score:0;
    //     }

    //     // $data['refund_fee'] = $refund_fee > 0 ? $refund_fee : $order_item['pay_fee'];



    //     if ($aftertype_id) {
    //         $data['type_str'] = Db::name('aftertype')->where('id', '=', $aftertype_id)->value('name');
    //     } else {
    //         return $this->error('请选择售后类型');
    //     }

    //     $data['aftersale_sn'] = rand_order('Refund' . $id, $this->auth->user()->id); //售后单号


    //     $data['phone'] = $order_data['phone'];
    //     $data['ps'] = $ps;

    //     $data['goods_id'] = $order_item['goods_id'];
    //     $data['goods_sku_price_id'] = $order_item['goods_sku_price_id'];

    //     $data['goods_sku_text'] = $order_item['goods_sku_text'];
    //     $data['goods_title'] = $order_item['goods_title'];
    //     $data['goods_image'] = $order_item['goods_image'];

    //     $data['goods_original_price'] = $order_item['goods_original_price'];
    //     $data['discount_fee'] = $order_item['discount_fee'];
    //     $data['goods_price'] = $order_item['goods_price'];
    //     $data['goods_num'] = $order_item['goods_num'];
    //     $data['dispatch_status'] = $order_item['dispatch_status']; //发货状态:0=未发货,1=已发货,2=已收货
    //     $data['dispatch_fee'] = $order_item['dispatch_fee']; //发货费用
    //     $data['aftersale_status'] = '0'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
    //     $data['refund_status'] = '0'; //退款状态:-1=拒绝退款,0=未退款,1=同意
    //     $data['store_id'] = $order_item['store_id'];


    //     $data['createtime'] = time();

    //     $res = Db::name('order_aftersale')->insert($data);
    //     Db::name('order_item')->where('id', '=', $id)->update(['aftersale_status' => '1', 'updatetime' => time()]);
    //     if ($res) {

    //         $order = Db::name('order')->where('id', '=', $order_item['order_id'])->find();
    //         //直接加余额 积分 贝壳退回
    //         $订单操作['order_id'] = $order_item['order_id'];
    //         $订单操作['order_item_id'] = $order_item['id'];
    //         $订单操作['oper_type'] = 'user'; //user','store','admin','system'
    //         $订单操作['oper_id'] = $this->auth->user()->id;
    //         $订单操作['order_status'] = $order['status']; //订单状态
    //         $订单操作['dispatch_status'] = '1'; //发货状态:0=未发货,1=已发货,2=已收货
    //         $订单操作['comment_status'] = $order_item['comment_status']; //评价状态:0=未评价,1=已评价
    //         $订单操作['aftersale_status'] = $order_item['aftersale_status']; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
    //         $订单操作['refund_status'] = $order_item['refund_status']; //退款状态:-1=拒绝退款,0=未退款,1=同意
    //         $订单操作['remark'] = '用户申请退款';
    //         $订单操作['createtime'] = time();
    //         $订单操作['updatetime'] = time();
    //         $this->UpdateOrderAction($订单操作);

    //         return $this->success('请等待商家处理');
    //     }

    //     // `aftersale_sn` varchar(40) NOT NULL COMMENT '售后单号',
    //     // `user_id` int(11) NOT NULL DEFAULT '0' COMMENT '用户',
    //     // `type` varchar(20) NOT NULL COMMENT '类型:refund=退款,return=退货,change=换货,other=其他',
    //     // `phone` varchar(20) DEFAULT NULL COMMENT '联系方式',
    //     // `activity_id` int(11) DEFAULT NULL COMMENT '活动',
    //     // `activity_type` varchar(255) DEFAULT NULL COMMENT '活动类型',
    //     // `order_id` int(11) NOT NULL DEFAULT '0' COMMENT '订单',
    //     // `order_item_id` int(11) NOT NULL DEFAULT '0' COMMENT '订单商品',
    //     // `goods_id` int(11) NOT NULL DEFAULT '0' COMMENT '商品',
    //     // `goods_sku_price_id` int(11) NOT NULL DEFAULT '0' COMMENT '规格 id',
    //     // `goods_sku_text` varchar(30) DEFAULT NULL COMMENT '规格名',
    //     // `goods_title` varchar(255) DEFAULT NULL COMMENT '商品名称',
    //     // `goods_image` varchar(255) DEFAULT NULL COMMENT '商品图片',
    //     // `goods_original_price` decimal(10,2) NOT NULL COMMENT '商品原价',
    //     // `discount_fee` decimal(10,2) DEFAULT NULL COMMENT '优惠费用',
    //     // `goods_price` decimal(10,2) NOT NULL COMMENT '商品价格',
    //     // `goods_num` int(11) NOT NULL DEFAULT '0' COMMENT '购买数量',
    //     // `dispatch_status` tinyint(2) NOT NULL DEFAULT '0' COMMENT '发货状态:0=未发货,1=已发货,2=已收货',
    //     // `dispatch_fee` decimal(10,2) DEFAULT NULL COMMENT '发货费用',
    //     // `aftersale_status` tinyint(2) NOT NULL COMMENT '售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成',
    //     // `refund_status` tinyint(1) DEFAULT NULL COMMENT '退款状态:-1=拒绝退款,0=未退款,1=同意',
    //     // `refund_fee` decimal(10,2) DEFAULT NULL COMMENT '退款金额',
    //     // `createtime` int(11) DEFAULT NULL COMMENT '添加时间',
    //     // $this->CancelTotalOrder($order_id);
    //     // return $this->success('取消成功',$order_id);
    // }





    // /**
    //  * 子订单申请售后
    //  *
    //  * @param int $id 订单id
    //  * @param string $ps 备注
    //  * @return void
    //  * @Action
    //  */
    // public function item_apply_aftersale(?int $id, $ps = null, $type = null, ?float $refund_fee = null, ?float $refund_score = null, ?float $refund_shell = null, ?int $aftertype_id = null, $images = null)
    // {
    //     $where['id'] = $id;
    //     $where['user_id'] =  $this->auth->user()->id;
    //     $order_item = Db::name('order_item')
    //         // ->whereEx($this->whereCom)
    //         ->whereEx($where)
    //         ->whereIsNull('deletetime')
    //         ->find();
    //     if (!$order_item)
    //         return $this->error('没有该订单');
    //     $order_data = Db::name('order')->where('id', '=', $order_item['order_id'])->find();



    //     $data['user_id'] = $this->auth->user()->id;
    //     $data['order_id'] = $order_data['id'];
    //     $data['order_item_id'] = $id;
    //     $data['type'] = 'refund'; //类型:refund=退款,return=退货,other=其他
    //     $res = Db::name('order_aftersale')->whereEx($data)->find();
    //     if ($res) {
    //         return $this->error('该订单已申请过退款');
    //     }

    //     if ($refund_fee > $order_item['item_pay_fee'] * $order_item['goods_num'] + $order_data['dispatch_amount']) {
    //         return $this->error('大于购买商品的价格的钱', $order_item['item_pay_fee'] * $order_item['goods_num'] + $order_data['dispatch_amount']);
    //         $data['refund_fee'] = $refund_fee;
    //     } elseif (!$refund_fee || $refund_fee < 0) {
    //         // return $this->error('请输入退款金额');
    //         $data['refund_fee'] = $order_item['item_pay_fee'] * $order_item['goods_num'];
    //     }

    //     if ($refund_shell > $order_item['item_shell_fee'] * $order_item['goods_num']) {
    //         return $this->error('大于购买商品的贝壳', $order_item['item_shell_fee'] * $order_item['goods_num']);
    //     } else {
    //         $data['refund_shell'] = $order_item['item_shell_fee'] * $order_item['goods_num'];
    //     }

    //     if ($refund_score > $order_item['item_score_fee'] * $order_item['goods_num']) {
    //         return $this->error('大于购买商品的积分', $order_item['item_score_fee'] * $order_item['goods_num']);
    //     } else {
    //         $data['refund_score'] = $order_item['item_score_fee'] * $order_item['goods_num'];
    //         // $data['refund_score'] = $refund_score?$refund_score:0;
    //     }

    //     // $data['refund_fee'] = $refund_fee > 0 ? $refund_fee : $order_item['pay_fee'];



    //     if ($aftertype_id) {
    //         $data['type_str'] = Db::name('aftertype')->where('id', '=', $aftertype_id)->value('name');
    //     } else {
    //         return $this->error('请选择售后类型');
    //     }

    //     $data['aftersale_sn'] = rand_order('Refund' . $id, $this->auth->user()->id); //售后单号

    //     $data['images'] = $images;
    //     $data['phone'] = $order_data['phone'];
    //     $data['ps'] = $this->Xss($ps);

    //     $data['goods_id'] = $order_item['goods_id'];
    //     $data['goods_sku_price_id'] = $order_item['goods_sku_price_id'];

    //     $data['goods_sku_text'] = $order_item['goods_sku_text'];
    //     $data['goods_title'] = $order_item['goods_title'];
    //     $data['goods_image'] = $order_item['goods_image'];

    //     $data['goods_original_price'] = $order_item['goods_original_price'];
    //     $data['discount_fee'] = $order_item['discount_fee'];
    //     $data['goods_price'] = $order_item['goods_price'];
    //     $data['goods_num'] = $order_item['goods_num'];
    //     $data['dispatch_status'] = $order_item['dispatch_status']; //发货状态:0=未发货,1=已发货,2=已收货
    //     $data['dispatch_fee'] = $order_item['dispatch_fee']; //发货费用
    //     $data['aftersale_status'] = '0'; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
    //     $data['refund_status'] = '0'; //退款状态:-1=拒绝退款,0=未退款,1=同意
    //     $data['store_id'] = $order_item['store_id'];


    //     $data['createtime'] = time();

    //     $res = Db::name('order_aftersale')->insert($data);
    //     Db::name('order_item')->where('id', '=', $id)->update(['aftersale_status' => '1', 'updatetime' => time()]);
    //     if ($res) {

    //         $order = Db::name('order')->where('id', '=', $order_item['order_id'])->find();
    //         //直接加余额 积分 贝壳退回
    //         $订单操作['order_id'] = $order_item['order_id'];
    //         $订单操作['order_item_id'] = $order_item['id'];
    //         $订单操作['oper_type'] = 'user'; //user','store','admin','system'
    //         $订单操作['oper_id'] = $this->auth->user()->id;
    //         $订单操作['order_status'] = $order['status']; //订单状态
    //         $订单操作['dispatch_status'] = '1'; //发货状态:0=未发货,1=已发货,2=已收货
    //         $订单操作['comment_status'] = $order_item['comment_status']; //评价状态:0=未评价,1=已评价
    //         $订单操作['aftersale_status'] = $order_item['aftersale_status']; //售后状态:-1=拒绝,0=未处理,1=处理中,2=售后完成
    //         $订单操作['refund_status'] = $order_item['refund_status']; //退款状态:-1=拒绝退款,0=未退款,1=同意
    //         $订单操作['remark'] = '用户申请退款';
    //         $订单操作['createtime'] = time();
    //         $订单操作['updatetime'] = time();
    //         $this->UpdateOrderAction($订单操作);

    //         return $this->success('请等待商家处理');
    //     }
    // }

}
